//
//  KKBaseItemList.m
//  FreightAssistant
//
//  Created by leo on 15/11/3.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKBaseItemList.h"

@implementation KKBaseItemList

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _itemArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 

- (NSInteger)count {
    
    return _itemArray.count;
}

- (void)appendItemsWithResponse:(NSDictionary*)response withPageIndex:(NSInteger)pageIndex {
    
    [self appendItemsWithResponse: response withPageIndex: pageIndex withItemClass: [KKBaseItem class]];
}

- (void)appendItemsWithResponse:(NSDictionary*)response withPageIndex:(NSInteger)pageIndex  withItemClass:(Class)itemClass {
    
    if (![itemClass isSubclassOfClass: [KKBaseItem class]]) {
        return;
    }
    
    if (pageIndex <= 1) {
        [_itemArray removeAllObjects];
    }
    
    self.pageIndex = pageIndex;
    
    NSArray *items = response[@"list"];
    if ([items isKindOfClass: [NSArray class]]) {
        
        for (NSDictionary *itemDictionary in items) {
            
            KKBaseItem* item = [itemClass new];
            [item kk_parserJsonDictionary: itemDictionary];
            [_itemArray addObject: item];
        }
    }
}

- (void)appendItemsWithArray:(NSArray*)itemArray {
    
    [self appendItemsWithArray: itemArray withItemClass: [KKBaseItem class]];
}

- (void)appendItemsWithArray:(NSArray*)itemArray withItemClass:(Class)itemClass {
    
    if (![itemClass isSubclassOfClass: [KKBaseItem class]]) {
        return;
    }
    
    [_itemArray removeAllObjects];
    
    if ([itemArray isKindOfClass: [NSArray class]]) {
        
        for (NSDictionary *itemDictionary in itemArray) {
            
            KKBaseItem* item = [itemClass new];
            [item kk_parserJsonDictionary: itemDictionary];
            [_itemArray addObject: item];
        }
    }
}

#pragma mark - 

- (id)objectAtIndex:(NSUInteger)index {
    
    if (index < _itemArray.count) {
        return _itemArray[index];
    }
    return nil;
}

- (void)removeObject:(id)anObject {
    
    if ([_itemArray containsObject: anObject]) {
        [_itemArray removeObject: anObject];
    }
}

- (void)removeObjectOfIndex:(NSUInteger)index {
    
    if (index < _itemArray.count) {
        [_itemArray removeObjectAtIndex: index];
    }
}

- (void)removeAllObjects {
    
    [_itemArray removeAllObjects];
}

@end


#pragma mark - KKBaseItem methods

@implementation KKBaseItem

- (NSString* )kk_valueForKey:(NSString *)key {
    
    if (key != nil && [key isKindOfClass: [NSString class]]) {
        return [self valueForKey: [NSString stringWithFormat: @"kk_%@", key]];
    }
    return nil;
}

#pragma mark - NSCoping methods

- (instancetype)copyWithZone:(NSZone *)zone {
    
    KKBaseItem *copyItem = [[KKBaseItem allocWithZone: zone] init];
    
    NSObject *targetObject = copyItem;
    
    while (targetObject.class != [NSObject class]) {
        
        unsigned int count = 0;
        //成员变量
        Ivar *ivars = class_copyIvarList(targetObject.class, &count);
        
        for (int i = 0; i < count; i++) {
            
            Ivar ivar = ivars[i];
            
            //  变量名称
            NSString *ivarName = [[NSString alloc] initWithUTF8String: ivar_getName(ivar)];
            
            NSString* key = ivarName;
            ivarName = [ivarName substringFromIndex: 1];
            
            key = [key substringFromIndex: 1];
            
            //  获取值
            id value = [self valueForKey: key];
            if (value != nil && ![value isKindOfClass: [NSNull class]]) {
                [copyItem setValue: value forKey: ivarName];
            }
        }
        free(ivars);
        
        targetObject = [targetObject.superclass new];
    }
    
    return copyItem;
}

#pragma mark - NSCoding methods

- (void)encodeWithCoder:(NSCoder *)aCoder {
    
    unsigned int count = 0;
    //成员变量
    objc_property_t *properties =class_copyPropertyList(self.class, &count);
    
    for (int i = 0; i < count; i++) {
        
        objc_property_t property = properties[i];
        NSString* ivarName = [[NSString alloc] initWithUTF8String: property_getName(property)];
        NSString* key = ivarName;
        
        id value = [self valueForKey: key];
        if (value != nil && ![value isKindOfClass: [NSNull class]]) {
            
            [aCoder encodeObject: value forKey: key];
        }
    }
    free(properties);
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super init];
    if (self) {
        
        unsigned int count = 0;
        //成员变量
        objc_property_t *properties =class_copyPropertyList(self.class, &count);
        
        for (int i = 0; i < count; i++) {
            
            objc_property_t property = properties[i];
            NSString* ivarName = [[NSString alloc] initWithUTF8String: property_getName(property)];
            NSString* key = ivarName;
            
            id value = [aDecoder decodeObjectForKey: key];
            if (value != nil && ![value isKindOfClass: [NSNull class]]) {
                [self setValue: value forKey: key];
            }
        }
        free(properties);
        
    }
    return self;
}

@end